///////////////////////////////////////////////////////////////////////
//  CS 3716 (Winter 2012), Assignment #4                       		 //
//  Program File Name: UserDatabase.java                      		 //
//       Student Name: Jonathan Hart-Wall, Adam Murphy         		 //
//					   Yifu Liang, Joshua Dowling			   		 //
//         Login Name: jcwhw1, agm264 , n96yl, n23jdd                //
//              MUN #: 200933455, 200933133, 200565679, 200703684    //
///////////////////////////////////////////////////////////////////////

import java.io.*;
import java.util.ArrayList;

public class UserDatabase{
	
	private File userDatabaseFile;
	private ArrayList<User> userDatabase;
    
	/**
	 * UserDatabase Constructor
	 * @param fileLocation
	 */
	public UserDatabase(String fileLocation){
		userDatabaseFile = new File(fileLocation);
		userDatabase = new ArrayList<User>();
	}
	
	/**
	 * Load method to create UserDatabase.
	 * Method reads an external text file line by line, and creates a new User Object from Strings on line.
	 * The User is then added to the ArrayList<User> userDatabase.
	 */
	public void load() throws FileNotFoundException, IOException{
		if (!userDatabaseFile.exists())
		{
			userDatabaseFile.createNewFile();
		}
	
        //if no user database directory or user file exists, throw some sort of no user exception
		BufferedReader br = new BufferedReader(new FileReader(userDatabaseFile));
		String line = "";
			
		while ((line = br.readLine()) != null)
		{
			String[] userParams = line.split("#");
			userDatabase.add(new User(userParams[0], userParams[1], userParams[2]));
		}
		br.close();
	}
    
	/**
	 * Accessor method to return User from userDatabase.
	 * Method searches userDatabase for User with String name.
	 * If found, returns User object, else returns NoSuchUserException
	 */
    public User getUser(String name) throws NoSuchUserException{
        for(User u : userDatabase){
            if(u.getName().equals(name)) return u;
        }
        throw new NoSuchUserException("Invalid user name");
    }
    
    /**
     * Mutator method to add User to userDatabase.
     * Method searches userDatabase for User with String name.
     * If found, throws UserNameTakenException, else creates new User with
     * parameters name, password, and type, and adds it to userDatabase.
     */
    public void addUser(String name, String password , String type)throws UserNameTakenException{
        for(User u: userDatabase){
            if(u.getName().equals(name)) throw new UserNameTakenException("The users name has already existed!");
        }
        userDatabase.add(new User(name, password, type));
    }
    
    /**
     * Method to delete User from userDatabase.
     * Method searches for User with String name.
     * If found, removes User with String name from userDatabase and breaks loop.
     * else, throws NoSuchUserexception
     */
    public void deleteUser(String name) throws NoSuchUserException{
        for(int i = 0; i < userDatabase.size(); i++){
        	if (userDatabase.get(i).getName().equals(name)){
                userDatabase.remove(i);
                return;
            }
        }
        throw new NoSuchUserException("Invalid user name!");
    }
    
    /**
	 * Method to update all contents of UserDatabase to an external text file.
	 */
    public void update() throws FileNotFoundException
    {
    	PrintWriter pw = new PrintWriter(userDatabaseFile);
    	for (User user : userDatabase)
    		pw.println(user.getName() + "#" + user.getPassword() + "#" + user.getType());
    	pw.close();
    }
    
    /**
     * Boolean method to determine whether User with String username is verified.
     * Method searches userDatabase for User with String userName and String password.
     * If found, returns true, else returns false.
     */
    public boolean verify(String username, String password){
    	for(int i = 0; i < userDatabase.size(); i++){
    		if(username.equals(userDatabase.get(i).getName()) && password.equals(userDatabase.get(i).getPassword())){
    			return true;
    		}
    		}
    	return false;
    	}
    
    public boolean isEmpty(){
    	if (userDatabase.size() == 0){
    		return true;
    	}
    	else{
    		return false;
    	}
    }
}
